package com.soft.small.util.encrypt;



import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.soft.small.app.config.APPConfig;
import com.soft.util.encrypt.Md5;
import lombok.extern.slf4j.Slf4j;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

@Slf4j
public class SignUtil {
    public static byte[] compress(byte[] data) {
        Deflater compresser = new Deflater();
        compresser.setInput(data);
        compresser.finish();
        ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
        try {
            final byte[] buf = new byte[1024];
            int i;
            while ((i = compresser.deflate(buf))>0) {
                bos.write(buf, 0, i);
            }
            return  bos.toByteArray();
        } finally {
            try {
                if (bos!=null) {
                    bos.close();

                }
                compresser.end();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static byte[] decompress(byte[] data) {
        Inflater decompresser = new Inflater();
        decompresser.setInput(data);
        ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);
        try {
            final byte[] buf = new byte[1024];
            int i;
            while ((i = decompresser.inflate(buf))>0) {
                o.write(buf, 0, i);
            }
            return o.toByteArray();
        } catch (Exception e) {
            return new byte[0];
        } finally {
            if (o!= null) {
                try {
                    o.close();
                    o= null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            decompresser.end();
        }
    }
    public static byte[] encrySign(String message, String appSecret){

        try {
            String sign = Md5.sign(message,appSecret,"");
            JSONObject json = JSONUtil.parseObj(message);
            byte[] compress = compress(Aes.encryptAes3(json.toString(), appSecret));
            return  getPublishByte(compress, sign);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }
    private static final String UIOT = "UIOT";

    public static byte[] getPublishByte(byte[] content,String signstr) {
        byte[] newbyte = new byte[content.length + 78];

        byte[]header = new byte[78];

        byte[] headerbyte5 = UIOT.getBytes();

        byte[]versionbyte1 = new byte[1];
        versionbyte1[0] = 1;

        byte[]appkeybyte33 = APPConfig.CHK_appKey.getBytes();
        byte[]encflagbyte1 =  new byte[1];
        encflagbyte1[0] = 1;
        byte[]compressbyte1 =  new byte[1];
        compressbyte1[0] = 1;

        byte[]tailbyte4 =  "EOS".getBytes();

        System.arraycopy(headerbyte5,0,header,0,headerbyte5.length);
        System.arraycopy(versionbyte1,0,header,5,1);
        System.arraycopy(appkeybyte33,0,header,6,appkeybyte33.length );
        System.arraycopy(encflagbyte1,0,header,39,1);
        System.arraycopy(compressbyte1,0,header,40,1);
        System.arraycopy(signstr.getBytes(),0,header,41,signstr.getBytes().length);
        System.arraycopy(tailbyte4,0,header,74,tailbyte4.length);

        System.arraycopy(header,0,newbyte,0,header.length);
        System.arraycopy(content,0,newbyte,header.length,content.length);
        return newbyte;
    }

    public static byte[] getPublishMessage(String message,String appSecret) {
        return  encrySign(message,appSecret);
    }
}
